package com.dmap.admin.service.backgroundv3.Impl;

import com.dmap.admin.controller.backgroundv3.param.FirmwareVersionEditParam;
import com.dmap.admin.controller.backgroundv3.param.FirmwareVersionParam;
import com.dmap.admin.controller.backgroundv3.param.FirmwareVersionPublishParam;
import com.dmap.admin.dao.FirmwareVersionMapper;
import com.dmap.admin.po.FirmwareVersion;
import com.dmap.admin.po.FirmwareVersionExample;
import com.dmap.admin.service.backgroundv3.FirmwareVersionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author: zmh
 * @create: 18-8-22
 * @description: 硬件版本
 **/
@Service
public class FirmwareVersionSerivceImpl implements FirmwareVersionService {
    @Resource
    private FirmwareVersionMapper firmwareVersionMapper;
    @Transactional
    @Override
    public int insertFirmwareVersionVersion(FirmwareVersionParam param) {
        FirmwareVersion firmwareVersion = new FirmwareVersion();
        if (param.getUrl() != null) {
            firmwareVersion.setUrl(param.getUrl());
        }
        if (param.getStatus() != null) {
            firmwareVersion.setStatus(param.getStatus());
        }
        if (param.getNewVersionCode() != null) {
            firmwareVersion.setNewVersionCode(param.getNewVersionCode());
        }
        if (param.getNewVersion() != null) {
            firmwareVersion.setNewVersion(param.getNewVersion());
        }
        if (param.getMinVersionCode() != null) {
            firmwareVersion.setMinVersionCode(param.getMinVersionCode());
        }
        if (param.getMinVersion() != null) {
            firmwareVersion.setMinVersion(param.getMinVersion());
        }
        if (param.getMd5() != null) {
            firmwareVersion.setMd5(param.getMd5());
        }
        if (param.getJsonUrl() != null) {
            firmwareVersion.setJsonUrl(param.getJsonUrl());
        }
        if (param.getDescriptionEn() != null) {
            firmwareVersion.setDescriptionEn(param.getDescriptionEn());
        }
        if (param.getDescription() != null) {
            firmwareVersion.setDescription(param.getDescription());
        }
        if (param.getFirmwareNumber() != null) {
            firmwareVersion.setFirmwareNumber(param.getFirmwareNumber());
        }
        firmwareVersion.setCreateTime(System.currentTimeMillis()/1000);
        firmwareVersion.setUpdateTime(System.currentTimeMillis()/1000);
        firmwareVersion.setUploadTime(System.currentTimeMillis()/1000);
        return firmwareVersionMapper.insertSelective(firmwareVersion);

    }
    @Transactional
    @Override
    public int updateFirmwareVersion(FirmwareVersionEditParam param) {
        FirmwareVersion firmwareVersion = firmwareVersionMapper.selectByPrimaryKey(Long.parseLong(param.getFirmwareVersionId()));
        if (firmwareVersion == null) {
            //没有该version
            return 2;
        }
        if (param.getUrl() != null) {
            firmwareVersion.setUrl(param.getUrl());
        }
        if (param.getStatus() != null) {
            firmwareVersion.setStatus(param.getStatus());
        }
        if (param.getNewVersionCode() != null) {
            firmwareVersion.setNewVersionCode(param.getNewVersionCode());
        }
        if (param.getNewVersion() != null) {
            firmwareVersion.setNewVersion(param.getNewVersion());
        }
        if (param.getMinVersionCode() != null) {
            firmwareVersion.setMinVersionCode(param.getMinVersionCode());
        }
        if (param.getMinVersion() != null) {
            firmwareVersion.setMinVersion(param.getMinVersion());
        }
        if (param.getMd5() != null) {
            firmwareVersion.setMd5(param.getMd5());
        }
        if (param.getJsonUrl() != null) {
            firmwareVersion.setJsonUrl(param.getJsonUrl());
        }
        if (param.getDescriptionEn() != null) {
            firmwareVersion.setDescriptionEn(param.getDescriptionEn());
        }
        if (param.getDescription() != null) {
            firmwareVersion.setDescription(param.getDescription());
        }
        if (param.getFirmwareNumber() != null) {
            firmwareVersion.setFirmwareNumber(param.getFirmwareNumber());
        }
        firmwareVersion.setUpdateTime(System.currentTimeMillis()/1000);
        firmwareVersion.setUploadTime(System.currentTimeMillis()/1000);
        return firmwareVersionMapper.updateByPrimaryKeySelective(firmwareVersion);
    }

    @Override
    public int deleteFirmwareVersion(long id) {
        FirmwareVersion firmwareVersion = firmwareVersionMapper.selectByPrimaryKey(id);
        if (firmwareVersion == null) {
            return 0;
        }
        return firmwareVersionMapper.deleteByPrimaryKey(id);

    }
    @Transactional
    @Override
    public int deleteFirmwareVersionList(String ids) {
        List<String> firmwareVersionList = Arrays.asList(ids.split(","));
        int count = 0;
        for (String id : firmwareVersionList) {
            count += deleteFirmwareVersion(Long.parseLong(id));
        }
        if (count == firmwareVersionList.size()) {
            return 1;
        }
        return 0;
    }
    @Transactional
    @Override
    public int updateFirmwareVersionPublish(FirmwareVersionPublishParam param) {
        FirmwareVersion firmwareVersion = firmwareVersionMapper.selectByPrimaryKey(Long.parseLong(param.getFirmwareVersionId()));
        if (firmwareVersion == null) {
            //没有该version
            return 2;
        }
        //1.代表正常  2代表禁用
        firmwareVersion.setStatus(1);
        firmwareVersion.setCreateTime(System.currentTimeMillis()/1000);
        return firmwareVersionMapper.updateByPrimaryKeySelective(firmwareVersion);

    }

    @Override
    public List<FirmwareVersion> selectFirmwareVersion() {
        FirmwareVersionExample firmwareVersionExample = new FirmwareVersionExample();
        FirmwareVersionExample.Criteria criteria = firmwareVersionExample.createCriteria();
        //增加条件
        return firmwareVersionMapper.selectByExample(firmwareVersionExample);
    }

    @Override
    public int countFirmwareVersion() {
        FirmwareVersionExample firmwareVersionExample = new FirmwareVersionExample();
        FirmwareVersionExample.Criteria criteria = firmwareVersionExample.createCriteria();
        //增加条件
        return firmwareVersionMapper.countByExample(firmwareVersionExample);

    }

    @Override
    public FirmwareVersion getFirmwareVersion(long id) {
        return firmwareVersionMapper.selectByPrimaryKey(id);
    }
}
